package site.overview.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.system.entity.Contact;
import modules.system.entity.User;
import modules.system.form.ContactForm;
import modules.system.service.ContactService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;
import com.guinea.core.utils.CommonUtils;
import com.guinea.core.utils.StringUtils;
import com.guinea.core.web.StrutsSimpleAction;

import ex.system.service.UserServiceEx;

public class ContactActionSite extends StrutsSimpleAction {

	public static final String LIST = "success";

	public static final String EDIT = "edit";

	Long curUserId;

	Long userId;

	User user;

	UserServiceEx userServiceEx;

	ContactService contactService;

	public EntityService getEntityService() {
		return contactService;
	}

	public ContactService getContactService() {
		return contactService;
	}

	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}

	public UserServiceEx getUserServiceEx() {
		return userServiceEx;
	}

	public void setUserServiceEx(UserServiceEx userServiceEx) {
		this.userServiceEx = userServiceEx;
	}

	protected void initProperties() {
		super.initProperties();
		curUserId = null;
		userId = null;
		user = null;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		curUserId = CommonUtils.getCurrentUserId(request);
		userId = curUserId;
		if (userId == null || userId.longValue() < 1) {
			String userIdStr = request.getParameter("userId");
			if (!StringUtils.isEmpty(userIdStr))
				userId = new Long(userIdStr);
		}
		if (userId != null && userId.longValue() > 0)
			user = getUserServiceEx().getUser(userId);
		if (user != null)
			entity = user.getContact();
	}

	/**
	 * url参数未定义method时的默认Action函数. 默认为index Action.
	 */
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return edit(mapping, form, request, response);
	}

	/**
	 * 保存前的验证.
	 */
	public ActionForward editValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form, request);
		if (user == null || (curUserId == null || curUserId.longValue() < 1)
				&& entity != null) {
			valid = false;
			return mapping.findForward(SUCCESS);
		}
		return null;
	}

	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		bindForm(form, entity);
		if (isCreateOperation) {
			ContactForm contactForm = (ContactForm) form;
			contactForm.setUserId(userId);
			form = contactForm;
		}

		return handleEdit(mapping, form, request, response);
	}

	/**
	 * 保存前的验证.
	 */
	public ActionForward saveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (isCancelled(request)) {
			valid = false;
			return mapping.findForward(LIST);
		}

		if (!validateToken(request))
			return handleToken(mapping, form, request, response);

		initFirst(form, request);
		if (user == null || (curUserId == null || curUserId.longValue() < 1)
				&& entity != null) {
			valid = false;
			return mapping.findForward(SUCCESS);
		}
//		if (!validateForm(mapping, form, request))
//			return handleFormErrors(mapping, form, request, response);
		return null;
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		super.doInitEntity(request, form);
		if(isCreateOperation){
			Contact contact = (Contact)entity;
			contact.setUser(user);
			entity = contact;
		}
		
		doSaveEntity(request, form);

		saveMessage(request, "entity.saved");
		return mapping.findForward(SUCCESS);
	}

	protected ActionForward handleToken(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		saveError(request, "submit.duplicated");
		return handleEdit(mapping, form, request, response);
	}

	protected ActionForward handleFormErrors(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		saveErrors(request, formErrors);
		return handleEdit(mapping, form, request, response);
	}

	protected ActionForward handleEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		saveToken(request);
		return mapping.findForward(EDIT);
	}

}
